/**
 * 
 */

window.onload=function() {
		generateTable('playerTable', 'playerField');
		generateTable('enemyTable', 'enemyField');		
};

var clicks = 1;

function placeShips(){
	document.getElementById("playerField").setAttribute("class", "over");
	$(document).ready(function(){
		$('body').mouseout(function(){
			$('#mycursor').hide();
			return false;
		});
		$('body').mouseover(function(){
			$('#mycursor').show();
			return false;
		});
		$('body').mousemove(function(e){
			document.getElementById("body").setAttribute("class", "over");
			window.mouseX = e.pageX;
		    window.mouseY = e.pageY;
		    mousemov();
		});
		
		CellHandlers();		
	});	
};

function mousemov() { 
	$('#mycursor').css('left', window.mouseX).css('top', window.mouseY);
}


function CellHandlers() {
	var rows = document.getElementById('playerTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; i < rows.length; i++) {
		console.log("rida" + i);
		rows[i].id = i + 1;
		for (var j = 0; j < 10; j++){
			console.log("veerg" + j);
			var cells = rows[i].getElementsByTagName("td");
			cells[j].id = j + 1;
			cells[j].onclick = function() {
				var c = this.id;
				var r = this.parentNode.id;
				//alert("Current position: " + r + ", " + c);
				checkPosition(this);
				//this.style.backgroundImage = 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/batt6.gif")';
			}; 
		};
	};
}

function removeHandler(){
	var rows = document.getElementById('playerTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; i < rows.length; i++) {
		rows[i].id = i + 1;
		for (var j = 0; j < 10; j++){
			var cells = rows[i].getElementsByTagName("td");
			cells[j].id = j + 1;
			cells[j].onclick = function() {
				return;
			}; 
		};
	};
}

var tF;
function checkPosition(elem) {
	switch(clicks){
		case 1:
			legalPlace(elem, 5);
			if(elem.id < 7 && tF == 1){
				addShip(elem, 5);	
				$('#mycursor').css('background', 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/p4h.gif") no-repeat');
				$('#mycursor').css('width', '64px').css('height', '16px');
				clicks++;
			}
			tF = 1;
			break;
		case 2:
			legalPlace(elem, 4);
			if(elem.id < 8 && tF == 1){
				addShip(elem, 4);
				$('#mycursor').css('background', 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/p3h.gif") no-repeat');
				$('#mycursor').css('width', '48px').css('height', '16px');
				clicks++;
			}
			tF = 1;
			break;
		case 3:
			legalPlace(elem, 3);
			if(elem.id < 9 && tF == 1){
				addShip(elem, 3);
				$('#mycursor').css('background', 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/p3h.gif") no-repeat');
				$('#mycursor').css('width', '48px').css('height', '16px');
				clicks++;
			}
			tF = 1;
			break;
		case 4:
			legalPlace(elem, 3);
			if(elem.id < 9 && tF == 1){
				addShip(elem, 3);
				$('#mycursor').css('background', 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/p2h.gif") no-repeat');
				$('#mycursor').css('width', '48px').css('height', '16px');
				clicks++;
			}
			tF = 1;
			break;
		case 5:
			legalPlace(elem, 2);
			if(elem.id < 10 && tF == 1){
				addShip(elem, 2);
				clicks++;
				removeHandler();
				$('#mycursor').css('background', 'transparent');
				$('#mycursor').css('cursor','auto');
			}
			tF = 1;
			break;
	}
}

function addShip(el, x){
	var rows = document.getElementById('playerTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; i < rows.length; i++) {
	   rows[i].id = i + 1;
	   for (var j = 0; j < 10; j++){
		   var cells = rows[i].getElementsByTagName("td");
		   cells[j].id = j + 1;
	   }
	}
	var c = el.id-1;
	var r = el.parentNode.id-1;
	for (var i = 0; i < x; i++){		
		rows[r].cells[c].style.backgroundColor = 'black';
		c++;
	}
}

function legalPlace(el, x){
	var rows = document.getElementById('playerTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; i < rows.length; i++) {
	   rows[i].id = i + 1;
	   for (var j = 0; j < 10; j++){
		   var cells = rows[i].getElementsByTagName("td");
		   cells[j].id = j + 1;
	   };
	};
	var c = el.id-1;
	var r = el.parentNode.id-1;
	for (var i = 0; i < x; i++){		
		if(rows[c].cells[r].style.backgroundColor == 'black'){
			tF = 0;
		} else if (r>0){
			if(rows[r-1].cells[c].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (c>0){
			if(rows[r].cells[c-1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r>0 && c>0){
			if(rows[r-1].cells[c-1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r>0 && c<9){
			if(rows[r-1].cells[c+1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r<9){
			if(rows[r+1].cells[c].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (c<9){
			if(rows[r].cells[c+1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r<9 && c>0){
			if(rows[r+1].cells[c-1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r<9 && c<9){
			if(rows[r+1].cells[c+1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else if (r>0 && c<9){
			if(rows[r-1].cells[c+1].style.backgroundColor == 'black'){
				tF = 0;
			};
		} else {
			c++;
		};
	};
};

function generateTable(tableId, elementId) {
	row=new Array();
	cell=new Array();
	row_num=10; //edit this value to suit
	cell_num=10; //edit this value to suit
	enemy=document.createElement('table');
	enemy.setAttribute('id',tableId);
	tbo=document.createElement('tbody');
	for(c=0;c<row_num;c++){
		row[c]=document.createElement('tr');
		for(k=0;k<cell_num;k++) {
			cell[k]=document.createElement('td');
			row[c].appendChild(cell[k]);
		};
		tbo.appendChild(row[c]);
	};
	enemy.appendChild(tbo);
	document.getElementById(elementId).appendChild(enemy);
};

function navigate() {
	var buttonValue = document.getElementById("Strategy").value;
	if(buttonValue == "Place ships") {
		document.getElementById("Strategy").value = "Reset ships";
		setInterval(mousemov, 300);	
		placeShips();
	} else {
		clearTable();
		setInterval(mousemov, 300);	
		placeShips();
	};
};

function help(){
	alert("Guidelines for the battle... At the moment ships can be only added horisontally - marked with black (it's the evening)");
};

function confirmShips() {
	if(clicks < 6){
		if (clicks == 5){
			alert("You have " + 1 + " more ship to place!");
		} else{
			alert("You have " + (6-clicks) + " more ships to place!");
		};
	} else {
		var button1 = document.getElementById("Strategy");
		button1.parentNode.removeChild(button1);
		document.getElementById("Confirm").value = "Shoot";
	};
};

function clearTable() {
	$('#mycursor').css('background', 'url("http://i64.photobucket.com/albums/h161/PjurChaos/University%20work/new-p5h.gif") no-repeat');
	$('#mycursor').css('width', '80px').css('height', '16px');
	$('#mycursor').css('cursor','none');
	clicks=1;

	var rows = document.getElementById('playerTable').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; i < rows.length; i++) {
	   for (var j = 0; j < 10; j++){
		   var cells = rows[i].getElementsByTagName("td");
		   rows[i].cells[j].style.backgroundColor = '';
	   };
	};
};